Skip to content

fix: concatenate filepattern alias contents once per alias instead of once per flag#604

Merged
cspath1 merged 3 commits into
launchdarkly:mainfrom
Fieldguide:jaz/filepattern-alias-memory
Jul 15, 2026
Merged

fix: concatenate filepattern alias contents once per alias instead of once per flag#604
cspath1 merged 3 commits into
launchdarkly:mainfrom
Fieldguide:jaz/filepattern-alias-memory

Conversation

@jazanne

@jazanne jazanne commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Disclaimer: I used AI to generate this PR and fix - if that's not allowed please let me know.

Problem

GenerateAliases calls GenerateAliasesFromFilePattern once per flag key, and that function rebuilds the full concatenation of all glob-matched file contents (plus a []bytestring copy for the regex engine) on every call.

On a repo with ~80MB of matched source and ~160 flags, peak allocation reaches tens of GB and the process is OOM-killed (exit 137) before producing any output. The same blowup affects find-code-references-in-pull-request, which consumes this package as a library and passes all flag keys through the same path.

Fix

Hoist the concatenation out of the per-flag loop: GenerateAliases now builds each filepattern alias's contents once and reuses it across flags. GenerateAliasesFromFilePattern keeps its public signature and behavior, delegating to the extracted concatFilePatternContents + matchFilePatternAliases helpers.

With this change the same scan (~160 flags, ~80MB matched contents, alpine container) completes in ~14s with no OOM.

Testing

go test ./aliases/...

Existing suite passes unchanged; no behavior change, output is identical for the repos we compared before/after.

Prior art

#491 (for #377, "Tool gets stuck when defining glob filepattern on big repo") added glob and regex caching on this same path, which addressed the CPU side of big-repo filepattern scans. This change addresses the remaining memory side: the per-flag rebuild of the concatenated file contents.

GenerateAliases rebuilt the full concatenation of matched file contents
(plus a []byte-to-string copy) for every flag key. On a repo with ~80MB
of matched source and ~160 flags, peak allocation reached tens of GB and
the process was OOM-killed. Hoist the concatenation to GenerateAliases
and reuse it across flags.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

@cspath1 cspath1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this! It looks like there's a minor lint issue with allFileContents being unused now. Approving now so it can be merged once that's been fixed!

@jazanne
jazanne requested a review from cspath1 July 15, 2026 16:06
@cspath1
cspath1 merged commit d2b674b into launchdarkly:main Jul 15, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants